package com.mes.web.controller.jy;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.jy.Code;
import com.mes.entity.tdd.Staff;
import com.mes.service.jy.CodeServise;
import com.mes.utils.ResponseUtil;
import com.mes.utils.formatUtils;

/**
 * 条码CodeController
 * @author jyy
 * 2019-02-19 11:36:43
 */
@Controller
@RequestMapping("/code")
public class CodeController {

	@Resource
	private CodeServise codeServise;
	
	/**
	 * 条码保存
	 * @param code 条码实体
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/insert")
	public void insert(Code code, HttpSession session, HttpServletResponse response) throws Exception {
		
		 //从session中取出用户身份信息
        Staff staff = (Staff) session.getAttribute("staff");
		
		Date date = new Date();
		String entime = formatUtils.tempDateSecond(date);
		
		code.setEntime(entime);
		if(staff != null) {
			if(staff.getStaffname() != null) {
				code.setPeople(staff.getStaffname());
			}
		}
		
		int affact= codeServise.insert(code);
		ResponseUtil.write(response, affact);
	}
	
	/**
	 * 根据排产单详情 统计实际生产量
	 * @param did 排产单详情id
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectAct")
	public void selectAct(String did, HttpServletResponse response) throws Exception {
		int count = codeServise.selectAct(did);
		ResponseUtil.write(response, count);
	}
	
	/**
	 * 根据排产单明细id查询不良品
	 * @param did 排产单详情id
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectNogood")
	public void selectNogood(String did, HttpServletResponse response) throws Exception {
		int count = codeServise.selectNogood(did);
		ResponseUtil.write(response, count);
	}
	
	/**
	 * 根据条码统计
	 * @param code 条码
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectCode")
	public void selectCode(String code, HttpServletResponse response) throws Exception {
		int count = codeServise.selectCode(code);
		ResponseUtil.write(response, count);
	}
	
	/**
	 * 对不良登记的条码做校验
	 * @param code
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/checkNogoodCode")
	public void checkNogoodCode(String code, HttpServletResponse response) throws Exception {
		int count2 = 0;
		//检查是否是生产出来的产品
		int count = codeServise.countBycode(code);
		if(count >= 1) {
			//检查是否登记过
			count2 = codeServise.checkNogoodCode(code);
		}else {
			count2 = -1;
		}
		ResponseUtil.write(response, count2);
	}
	
}
